package org.dromara.article.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.dromara.article.domain.KeywordsDict;
import org.dromara.article.domain.vo.KeywordsDictQueryVo;
import org.dromara.article.mapper.KeywordsDictMapper;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.jetbrains.annotations.Nullable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Slf4j
@Service
public class KeyWordsService {

    @Resource
    private KeywordsDictMapper keywordsDictMapper;


    /**
     * 查询字典列表
     *
     * @param queryVo
     * @param pageQuery
     * @return
     */
    public TableDataInfo<KeywordsDict> list(KeywordsDictQueryVo queryVo, PageQuery pageQuery) {
        IPage<KeywordsDict> page = new Page<>(pageQuery.getPageNum(), pageQuery.getPageSize());
        IPage<KeywordsDict> pageResult = keywordsDictMapper.selectKeywordsList(queryVo, page);
        return TableDataInfo.build(pageResult);
    }

    /**
     * 添加字典
     *
     * @param keywordsDict
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public R<Void> add(KeywordsDict keywordsDict) {
        R<Void> 关键字不能为空 = verifyParams(keywordsDict);
        if (关键字不能为空 != null) return 关键字不能为空;

        keywordsDict.setStatus(1); // 关键词默认启用

        LoginUser loginUser = LoginHelper.getLoginUser();
        keywordsDict.setCreatedBy(loginUser.getUserId());
        keywordsDict.setUpdatedBy(loginUser.getUserId());
        keywordsDict.setCreateUser(loginUser.getUserId());
        keywordsDict.setCreateTime(new Date());
        keywordsDict.setUpdateTime(new Date());

        keywordsDictMapper.insert(keywordsDict);
        return R.ok("新增关键词成功");
    }

    /**
     * 校验字典参数信息
     *
     * @param keywordsDict
     * @return
     */
    @Nullable
    private static R<Void> verifyParams(KeywordsDict keywordsDict) {
        if (StrUtil.isBlank(keywordsDict.getKeywords())) {
            return R.fail("关键字不能为空");
        }
        if (null == keywordsDict.getTypeId()) {
            return R.fail("文章类型不能为空");
        }
        if (null == keywordsDict.getType()) {
            return R.fail("关键词类型不能为空");
        }
        return null;
    }

    /**
     * 更新字典信息
     *
     * @param keywordsDict
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public R<Void> update(KeywordsDict keywordsDict) {
        R<Void> verifyResult = verifyParams(keywordsDict);
        if (verifyResult.getCode() != 200) {
            return verifyResult;
        }

        KeywordsDict oldData = keywordsDictMapper.selectById(keywordsDict.getId());
        BeanUtil.copyProperties(keywordsDict, oldData);
        LoginUser loginUser = LoginHelper.getLoginUser();
        oldData.setUpdatedBy(loginUser.getUserId());
        oldData.setUpdateTime(new Date());

        keywordsDictMapper.updateById(oldData);
        return R.ok("关键词更新成功");
    }
}
